import {userLogin, userLogout} from '@/api/user'
import { userMenu } from '@/api/menu'
import adminRouter from "@/router/routes/admin";
import router from '@/router'


// state设置变量
// actions 异步-得到结果 - mutations 操作数据-getters监听数据变动
// 最后返回的是监听数据变动

export default {
    namespaced: true, // 是否开启命名空间
    // data 存储数据
    state: {
        user: {},
        token:'',
        adminMenu:[],
        permission:[],
    },
    // 异步
    actions: {
        // 登陆
        login({commit}, params) {
            return new Promise(resolve => {
                userLogin(params)
                    .then(res => {
                        if (Number(res.code) === 0) {
                            let data = res.data
                            commit('setToken', data.token)
                            commit('setUser', data.user)
                        }
                        resolve(res)
                    })
            })
        },
        // 退出登陆
        logout({commit}) {
           return new Promise(resolve => {
               userLogout()
                   .then(res => {
                       if (Number(res.code) === 0) {
                           commit('setToken', null)
                           commit('setUser', {})
                           commit('setMenu',null)
                           resolve()
                       }
                   })
           })
        },
        // 获取用户权限与菜单
        getMenuInfo({commit}){
            userMenu()
                .then(res=>{
                    let data = res.data
                    let permission = data.permission

                    commit('setPermission',permission)
                    // console.log(permission)
                    let adminMenuRouter = adminRouter(permission)
                    let adminChildUrlList = adminMenuRouter.children.map(v=>`/admin/${v.path}`)
                    let adminMenu = []
                    // console.log(adminChildUrlList)
                    for (let i = 0; i < data.adminMenu.length; i++) {
                        let item = data.adminMenu[i]
                        // 主页进入
                        if(item.url == adminMenuRouter.path){
                            adminMenu.push(item)
                            continue
                        }

                        if(item.children == 0){
                            adminChildUrlList.includes(`${item.url}`) &&  adminMenu.push(item)
                        }else{
                            // console.log(item)
                            let childMenuList = []
                            item.children.forEach(cItem=>{
                                adminChildUrlList.includes(cItem.url) &&  childMenuList.push(cItem)
                            })
                            let new_object = Object.create(item)
                            new_object.children = childMenuList
                            item.children.length>0 && adminMenu.push(new_object)
                        }
                    }

                    commit('setMenu',adminMenu)
                    router.addRoutes([adminMenuRouter])
                })
        },
    },
    //  操控 state 里面的数据的   methods
    mutations: {
        setUser(state, data) {
            state.user = data
        },
        setToken(state, data) {
            state.token = data
        },
        setMenu(state, data){
            state.adminMenu = data
        },
        setPermission(state, data){
            state.permission = data
        },
    },
    // Getters 可以用于监听、state中的值的变
    getters: {
        getUser: state => {
            return state.user
        },
        getToken: state => {
            return state.token
        },
        getMenu: state => {
            return state.adminMenu
        },
        getPermission: state => {
            return state.permission
        },
    },
}
